﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Globalization;

namespace Elentok.Common
{
  /// <summary>
  /// This class wraps around the "ConfigurationManager.AppSettings" object
  /// </summary>
  public static class AppConfig
  {
    /// <summary>
    /// Returns the value of the given key in the app.config file
    /// (or the value of <paramref name="defaultValue"/> if not found).
    /// </summary>
    /// <param name="key">The key of the appSettings item.</param>
    /// <param name="defaultValue">The default value to return if the key is not found.</param>
    /// <returns>The value of the <paramref name="key"/> in the app.config file 
    /// (or <paramref name="defaultValue"/> if it's not found).</returns>
    public static string GetString(string key, string defaultValue)
    {
      string value = ConfigurationManager.AppSettings[key];
      if (value == null)
        return defaultValue;

      return value;
    }

    /// <summary>
    /// Returns the value of the given key in the app.config.
    /// </summary>
    /// <param name="key">The key of the appSettings item.</param>
    /// <returns>The value of the <paramref name="key"/> in the app.config file.</returns>
    /// <exception cref="KeyNotFoundException">Thrown if the <paramref name="key"/> isn't found in the app.config.</exception>
    public static string GetString(string key)
    {
      string value = ConfigurationManager.AppSettings[key];
      if (value == null)
        throw new KeyNotFoundException(
          string.Format(CultureInfo.CurrentCulture, "The appSettings key '{0}' was not found", key));

      return value;
    }

    /// <summary>
    /// Returns the value of the given key in the app.config file
    /// (or the value of <paramref name="defaultValue"/> if not found).
    /// </summary>
    /// <param name="key">The key of the appSettings item.</param>
    /// <param name="defaultValue">The default value to return if the key is not found.</param>
    /// <returns>The value of the <paramref name="key"/> in the app.config file 
    /// (or <paramref name="defaultValue"/> if it's not found).</returns>
    public static bool GetBoolean(string key, bool defaultValue)
    {
      string value = GetString(key, null);
      if (value == null)
        return defaultValue;

      return bool.Parse(value);
    }

    /// <summary>
    /// Returns the value of the given key in the app.config.
    /// </summary>
    /// <param name="key">The key of the appSettings item.</param>
    /// <returns>The value of the <paramref name="key"/> in the app.config file.</returns>
    /// <exception cref="KeyNotFoundException">Thrown if the <paramref name="key"/> isn't found in the app.config.</exception>
    public static bool GetBoolean(string key)
    {
      return bool.Parse(GetString(key));
    }

    /// <summary>
    /// Returns the value of the given key in the app.config file
    /// (or the value of <paramref name="defaultValue"/> if not found).
    /// </summary>
    /// <param name="key">The key of the appSettings item.</param>
    /// <param name="defaultValue">The default value to return if the key is not found.</param>
    /// <returns>The value of the <paramref name="key"/> in the app.config file 
    /// (or <paramref name="defaultValue"/> if it's not found).</returns>
    public static int GetNumber(string key, int defaultValue)
    {
      string value = GetString(key, null);
      if (value == null)
        return defaultValue;

      return int.Parse(value, CultureInfo.CurrentCulture);
    }

    /// <summary>
    /// Returns the value of the given key in the app.config.
    /// </summary>
    /// <param name="key">The key of the appSettings item.</param>
    /// <returns>The value of the <paramref name="key"/> in the app.config file.</returns>
    /// <exception cref="KeyNotFoundException">Thrown if the <paramref name="key"/> isn't found in the app.config.</exception>
    public static int GetNumber(string key)
    {
      return int.Parse(GetString(key), CultureInfo.CurrentCulture);
    }
  }
}
